/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.validation;

import java.lang.annotation.Annotation;

import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.exceptions.LyciaConfigurationException;
import org.sourceprojects.lycia.internal.generator.MethodParameterUtils;
import org.sourceprojects.lycia.internal.platform.ParameterMatcher;
import org.w3c.dom.Element;

public class ParameterValidationField<B> implements ValidationField<B> {

	private final Annotation[] annotations;
	private final Class<?> parameter;
	private final Class<?> declaringClass;
	private final String name;

	ParameterValidationField(final Class<?> parameter,
			final Annotation[] annotations, final Class<?> declaringClass,
			final String name) {

		this.parameter = parameter;
		this.declaringClass = declaringClass;
		this.name = name;

		this.annotations = new Annotation[annotations.length];
		System.arraycopy(annotations, 0, this.annotations, 0,
				annotations.length);
	}

	@Override
	public Annotation[] getAnnotations() {
		final Annotation[] temp = new Annotation[annotations.length];
		System.arraycopy(annotations, 0, temp, 0, annotations.length);

		return temp;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Object getValue(final Element element,
			final LyciaParser<B> lyciaParser) {

		try {
			if (annotations.length > 0) {
				Object value = null;
				int size = annotations.length;
				int position = 0;

				for (final Annotation annotation : annotations) {
					final ParameterMatcher matcher = MethodParameterUtils
							.getParameterMatcher(parameter, annotation,
									lyciaParser);

					if (matcher != null) {
						boolean moreAnnotations = position++ < size - 1;

						if (value instanceof Element) {
							value = MethodParameterUtils
									.getParameterObjectByType(parameter,
											annotation, matcher, value,
											(Element) value,
											element.getLocalName(), null, null,
											lyciaParser, moreAnnotations);
						} else {
							value = MethodParameterUtils
									.getParameterObjectByType(parameter,
											annotation, matcher, value,
											element, element.getLocalName(),
											null, null, lyciaParser,
											moreAnnotations);

						}
					}
				}

				return value;
			} else {
				final ParameterMatcher matcher = MethodParameterUtils
						.getParameterMatcher(parameter, null, lyciaParser);

				return MethodParameterUtils.getParameterObjectByType(parameter,
						null, matcher, null, element, element.getLocalName(),
						null, null, lyciaParser, false);
			}

		} catch (final Exception e) {
			throw new LyciaConfigurationException(
					"An error has occured while extracting values for field validation",
					e);
		}
	}

	@Override
	public Class<?> getDeclaringClass() {
		return declaringClass;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((declaringClass == null) ? 0 : declaringClass.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((parameter == null) ? 0 : parameter.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final ParameterValidationField<?> other = (ParameterValidationField<?>) obj;
		if (declaringClass == null) {
			if (other.declaringClass != null) {
				return false;
			}
		} else if (!declaringClass.equals(other.declaringClass)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (parameter == null) {
			if (other.parameter != null) {
				return false;
			}
		} else if (!parameter.equals(other.parameter)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "ParameterValidationField [declaringClass=" + declaringClass
				+ ", name=" + name + ", parameter=" + parameter + "]";
	}

}
